package com.pearadmin.common.secure.support;

import com.pearadmin.common.secure.domain.SecureUserDetailsServiceImpl;
import com.pearadmin.common.secure.utils.JwtUtils;
import com.pearadmin.common.tools.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登 录 验 证 码 过 滤 器
 *
 * @author John Ming
 * @createTime 2020/11/20
 */
@Component
@Slf4j
public class SecureCaptchaSupport extends OncePerRequestFilter implements Filter {

    private String defaultFilterProcessUrl = "/login";
    private String method = "POST";

    /**
     * 验 证 码 校 监 逻 辑
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (method.equalsIgnoreCase(request.getMethod()) && defaultFilterProcessUrl.equals(request.getServletPath())) {
            //String captcha = ServletUtil.getRequest().getParameter("captcha");
            //response.setContentType("application/json;charset=UTF-8");
            // 下面注释 表示 不进行验证校验
            //if (StringUtil.isEmpty(captcha)) {
            //    response.getWriter().write(JSON.toJSONString(Result.failure("验证码不能为空!")));
            //    return;
            //}
            //if (!CaptchaUtil.ver(ServletUtil.getRequest().getParameter("captcha"), ServletUtil.getRequest())) {
            //    response.getWriter().write(JSON.toJSONString(Result.failure("验证码错误!")));
            //    return;
            //}
        }
        chain.doFilter(request, response);
    }
}